package com.vip.eshop.thread;

import com.vip.eshop.inventory.request.Request;
import com.vip.eshop.inventory.request.RequestQueue;

import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.ArrayList;

/**
 * Created by leo.song on 2017/8/25.
 */
public class RequestProcessorThreadPool {

    private static Integer POOL_SIZE = 10;
    private static Integer QUEUE_CAPACITY = 100;
    //in project, the size of thread pool should configure in file, and also the size of each queue.
    private ExecutorService threadPool = Executors.newFixedThreadPool(POOL_SIZE);


    public static RequestProcessorThreadPool getInstance() {
        return Singleton.getInstance();
    }

    public RequestProcessorThreadPool() {
        RequestQueue requestQueue = RequestQueue.getInstance();
        for(int i =0; i < POOL_SIZE; i++) {
            ArrayBlockingQueue<Request> queue = new ArrayBlockingQueue<Request>(QUEUE_CAPACITY);
            requestQueue.addQueue(queue);

            threadPool.submit(new WorkerThread(queue));

        }
    }

    public static void init() {
        getInstance();
    }
    private static class Singleton  {
        private static RequestProcessorThreadPool instance;

        static {
            instance = new RequestProcessorThreadPool();
        }

        public  static RequestProcessorThreadPool getInstance() {
            return instance;
        }
    }
}
